package com.rongc.client.core.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import com.rongc.client.freight.UniApplication;

import static com.rongc.client.core.utils.RegexUtil.checkNotNull;

public class ActivityUtils {

    public static void startActivity(Context context, Class<?> clz) {
        Intent intent;
        intent = new Intent(context, clz);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    public static void startActivityForResult(Activity context, Class<?> clz,int requestCode) {
        Intent intent;
        intent = new Intent(context, clz);
        context.startActivityForResult(intent,requestCode);
    }

    public static void startActivityNeedLogin(Context context, Class<?> clz) {

        startActivityNeedLogin(context,clz,null);
    }

    public static void startActivityNeedLogin(Context context, Class<?> clz, Bundle bundle) {

        Intent intent;
        if (UniApplication.getInstance().hasLogin()){

            intent = new Intent(context, clz);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            if (bundle != null) {
                intent.putExtras(bundle);
            }
            context.startActivity(intent);

        } else {

            /*intent = new Intent(context, LoginActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
            System.exit(0);*/
        }

    }

    public static void startActivity(Context context, Class<?> clz, Bundle bundle) {
        Intent intent = new Intent(context, clz);
        intent.putExtras(bundle);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    public static void jumpBrowser(Context context, String url) {
        if(RegexUtil.checkHttp(url)) {
            Uri uri = Uri.parse(url);
            Intent it = new Intent(Intent.ACTION_VIEW, uri);
            it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(it);
        }else{
            toast("网址不合法："+url);
        }
    }


    public static void toast(String content) {

        Toast.makeText(UniApplication.getInstance(), content, Toast.LENGTH_SHORT).show();
    }

    public static void closeInputKeyboard(Context context) {
        InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(((Activity) context).getCurrentFocus().getWindowToken(),

                InputMethodManager.HIDE_NOT_ALWAYS);
    }


    public static void mailTo(Context context, String mailAddress,
                              String subject, String text) {
        Intent data = new Intent(Intent.ACTION_SENDTO);
        data.setData(Uri.parse("mailto:" + mailAddress));
        data.putExtra(Intent.EXTRA_SUBJECT, subject);
        data.putExtra(Intent.EXTRA_TEXT, text);
        context.startActivity(data);
    }

    public static void callTo(Context context, String phoneNumber) {
        Intent phoneIntent = new Intent(Intent.ACTION_DIAL,

                Uri.parse("tel:" + phoneNumber));
        phoneIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(phoneIntent);
    }

    public static void toIntnet(Context context, String url) {
        Uri uri = Uri.parse(url);
        Intent urlIntent = new Intent(Intent.ACTION_VIEW, uri);
        context.startActivity(urlIntent);
    }

    public static void addFragmentToActivity (@NonNull FragmentManager fragmentManager,
                                              @NonNull Fragment fragment, int frameId) {
        checkNotNull(fragmentManager);
        checkNotNull(fragment);
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.add(frameId, fragment);
        transaction.commit();
    }

    public static void addFragmentToActivity (@NonNull FragmentManager fragmentManager,
                                              @NonNull Fragment fragment, int frameId, String tag) {
        checkNotNull(fragmentManager);
        checkNotNull(fragment);
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.add(frameId, fragment,tag);
        transaction.commit();
    }

    public static void replaceFragmentInActivity(@NonNull FragmentManager fragmentManager,
                                                 @NonNull Fragment fragment, int frameId) {

        checkNotNull(fragmentManager);
        checkNotNull(fragment);

        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(frameId, fragment);
        fragmentTransaction.commit();

    }

    public static void replaceFragmentInActivity(@NonNull FragmentManager fragmentManager,
                                                 @NonNull Fragment mCurrFragment,
                                                 @NonNull Fragment fragment, int frameId, String tag) {
        if(fragment == null)
            fragment = fragmentManager.findFragmentByTag(tag);

        checkNotNull(fragmentManager);
        checkNotNull(fragment);

        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
//        fragmentTransaction.replace(frameId, fragment);
//        fragmentTransaction.commit();

        if (!fragment.isAdded()) { // 先判断是否被add过
            if(mCurrFragment != null)
                fragmentTransaction.hide(mCurrFragment);
                fragmentTransaction.add(frameId, fragment,tag).commit();
        } else {
            fragmentTransaction.hide(mCurrFragment).show(fragment).commit();
        }
    }
}
